<?php
/**
 * Created by PhpStorm.
 * User: 田家强
 * Date: 2017/3/31
 * Time: 13:56
 */

namespace Home\Controller;
use Home\Model\AccountsModel;
use Org\Util\Cart;
use Think\Controller;
class AccountsController extends BaseContoller
{

    public function  __construct()
    {
        parent::__construct();
        if(!session('uid')){
            $this->error('请登录！',u('home/login/index'));
        }


    }
    public function index()
    {
        $headConf = ['title'=>'维依尚品_结算页','js'=>['list']];
        $this->assign('headConf',$headConf);
      //dd($_SESSION['payData']);
     //调用添加方法
        $this->Addaddress();
        //获取session里面的数据
        $payData  = $_SESSION['payData'];
        //dd($payData);
        $this->assign('payData',$payData);
        $address =m('accounts')->select();
        //dd($address);
        $this->assign('address',$address);
        $this->display();
        //获取数据库地址参数
    }

    /**
     * 异步执行添加编辑方法
     */
    public function Addaddress()
    {
        if(IS_AJAX)
        {
           $data = I('post.');
            if($data['oid']){
                $data['uaid']=$data['oid'];
            }
           //将三级联动地址分割成字符串入库  dd($data);die;
           $data['address']=implode(',',$data['area']).'_'.$data['address'];
           $data['uid']=$_SESSION['uid'];
           $this->store(new AccountsModel(),$data);
        }
    }
   /**
    * 异步执行下单
    */
    public function Immediately()
    {
        if(IS_AJAX){
          //dd($_POST);die;
            $data['consignee'] = $_POST['consignee'];//用户名称
            $data['address'] = $_POST['address'];    //送货地址
            $data['total'] = $_POST['total'];        //订单总价
            $data['phone'] = $_POST['phone'];        //手机号码
            $data['time'] = time();                   //新生成时间
//            $data['number'] = md5('tjq'+time());     //新生成订单号
            $data['number'] =Cart::getOrderId();
            $data['uid']=session('uid');             //抓取用户
            //dd($data);die;
            $oid =  m('order')->add($data);//执行添加方法

           //查找商品详细
            $data = ($_SESSION['payData']['goods']);
            //dd($data);
           // 获取属性规格值
          foreach($data as $k=>$v)
          {
              $temp ='';
             // foreach($v['options']['options'] as $kk =>$vv){
              //    $temp.=$vv.',';
             // }
            //dd($temp);
              $foodslist[] =[
               'number' =>Cart::getOrderId(),
               'quantity'=>$v['num'],//商品数量
               'subtotal'=>$v['price'],//价格小计
               'gid'=>$v['id'],//商品ID
               'oid'=>$oid,//订单ID
               'options'=>$temp//商品规格
              ];
          }
           //dd($foodslist);
            foreach($foodslist as $v){
            $res = m('order_list')->add($v);
          }
            if($res)
            {
                echo json_encode(['oid'=>$oid,'status'=>1,'info'=>'操作成功']);die;
            }
            $this->error('失败!');exit;

        }
     }
  }



















































